/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ClienteResultadoPesquisaGUI.java
 *
 * Created on 28/02/2010, 19:44:50
 */
package gui;

import excecao.AtributoNaoEncontrado;
import excecao.CPFInvalidoException;
import facade.AdvogadoFacade;
import javax.swing.JOptionPane;
import modelo.Cliente;

/**
 *
 * @author andrehadm
 */
public class ClienteResultadoPesquisaGUI extends javax.swing.JPanel {

    AdvogadoFacade advogadoFacade;
    Cliente cliente;

    /** Creates new form ClienteResultadoPesquisaGUI */
    public ClienteResultadoPesquisaGUI(Cliente cliente, AdvogadoFacade advogadoFacade) {
        this.advogadoFacade = advogadoFacade;
        this.cliente = cliente;
        initComponents();
        carregaCliente(cliente);
    }

    private void carregaCliente(Cliente cliente) {

        System.out.println("");
        System.out.println("Carregando cliente");

        //Nome
        jTextField_Nome.setText(cliente.getNome());

        //CPF
        jFormattedTextField_CPF.setText(cliente.getCpf());

        //Bairro
        if (!cliente.getBairro().isEmpty()) {
            jTextField_Bairro.setText(cliente.getBairro());
        }

        //Celular
        if (!cliente.getCelular().isEmpty()) {
            jFormattedTextField_Celular.setText(cliente.getCelular());
        }

        //Cep
        if (!cliente.getCep().isEmpty()) {
            jFormattedTextField_CEP.setText(cliente.getCep());
        }

        //Cidade
        if (!cliente.getCidade().isEmpty()) {
            jTextField_Cidade.setText(cliente.getCidade());
        }

        //Email
        if (!cliente.getEmail().isEmpty()) {
            jTextField_Email.setText(cliente.getEmail());
        }

        //Endereço
        if (!cliente.getEndereco().isEmpty()) {
            jTextField_Endereco.setText(cliente.getEndereco());
        }

        //RG
        if (!cliente.getRg().isEmpty()) {
            jTextField_RG.setText(cliente.getRg());
        }

        //Telefone
        if (!cliente.getTelefone().isEmpty()) {
            jFormattedTextField_Telefone.setText(cliente.getTelefone());
        }

        //Visita
        if (!cliente.getVisita().isEmpty()) {
            jFormattedTextField_Visista.setText(cliente.getVisita());
        }

        //Preposto
        if (!cliente.getPreposto().isEmpty()) {
            jTextField_Preposto.setText(cliente.getPreposto());
        }

        //UF
        jComboBox_UF.setSelectedItem(cliente.getUf());

        System.out.println("Cliente Carregado");
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jTextField_Email = new javax.swing.JTextField();
        jLabel_Celular = new javax.swing.JLabel();
        jLabel_Email = new javax.swing.JLabel();
        jLabel_UF = new javax.swing.JLabel();
        jLabel_Telefone = new javax.swing.JLabel();
        jLabel_CEP = new javax.swing.JLabel();
        jTextField_Bairro = new javax.swing.JTextField();
        jTextField_Cidade = new javax.swing.JTextField();
        jLabel_Visita = new javax.swing.JLabel();
        jComboBox_UF = new javax.swing.JComboBox();
        jFormattedTextField_CPF = new javax.swing.JFormattedTextField();
        jFormattedTextField_CEP = new javax.swing.JFormattedTextField();
        jFormattedTextField_Telefone = new javax.swing.JFormattedTextField();
        jFormattedTextField_Celular = new javax.swing.JFormattedTextField();
        jFormattedTextField_Visista = new javax.swing.JFormattedTextField();
        jLabel_CPF = new javax.swing.JLabel();
        jLabel_Nome = new javax.swing.JLabel();
        jLabel_Cidade = new javax.swing.JLabel();
        jTextField_RG = new javax.swing.JTextField();
        jLabel_RG = new javax.swing.JLabel();
        jTextField_Nome = new javax.swing.JTextField();
        jLabel_Endereco = new javax.swing.JLabel();
        jLabel_Bairro = new javax.swing.JLabel();
        jTextField_Endereco = new javax.swing.JTextField();
        jButton_AtualizarCliente = new javax.swing.JButton();
        jTextField_Preposto = new javax.swing.JTextField();
        jLabel_Preposto = new javax.swing.JLabel();

        setBorder(null);

        jLabel_Celular.setText("Celular");

        jLabel_Email.setText("Email");

        jLabel_UF.setText("UF");

        jLabel_Telefone.setText("Telefone");

        jLabel_CEP.setText("CEP");

        jLabel_Visita.setText("Próx. Visita");

        jComboBox_UF.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "RN", "AC", "AL", "AP", "AM", "BA", "CE", "DF", "GO", "ES", "MA", "MT", "MS", "MG", "PA", "PB", "PE", "PI", "RJ", "RS", "RO", "RR", "SP", "SC", "SE", "TO" }));

        try {
            jFormattedTextField_CPF.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("###.###.###-##")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jFormattedTextField_CEP.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("#####-###")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jFormattedTextField_Telefone.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jFormattedTextField_Celular.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("(##) ####-####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        try {
            jFormattedTextField_Visista.setFormatterFactory(new javax.swing.text.DefaultFormatterFactory(new javax.swing.text.MaskFormatter("##/##/####")));
        } catch (java.text.ParseException ex) {
            ex.printStackTrace();
        }

        jLabel_CPF.setText("CPF");

        jLabel_Nome.setText("Nome");

        jLabel_Cidade.setText("Cidade");

        jLabel_RG.setText("RG");

        jLabel_Endereco.setText("Endereço");

        jLabel_Bairro.setText("Bairro");

        jButton_AtualizarCliente.setText("Atualizar");
        jButton_AtualizarCliente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton_AtualizarClienteMouseClicked(evt);
            }
        });

        jLabel_Preposto.setText("Preposto");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel_Nome)
                            .addComponent(jLabel_CPF)
                            .addComponent(jLabel_Endereco)
                            .addComponent(jLabel_Bairro)
                            .addComponent(jLabel_Cidade)
                            .addComponent(jLabel_Telefone)
                            .addComponent(jLabel_CEP)
                            .addComponent(jLabel_Email))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextField_Bairro, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                            .addComponent(jTextField_Email, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jFormattedTextField_CPF, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel_RG)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jTextField_RG, javax.swing.GroupLayout.PREFERRED_SIZE, 117, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jTextField_Endereco, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                            .addComponent(jTextField_Nome, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jFormattedTextField_CEP, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jFormattedTextField_Telefone, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel_Celular)
                                    .addComponent(jLabel_UF))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jComboBox_UF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jLabel_Visita)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(jFormattedTextField_Visista, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addComponent(jFormattedTextField_Celular, javax.swing.GroupLayout.PREFERRED_SIZE, 114, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 95, Short.MAX_VALUE))
                            .addComponent(jTextField_Cidade, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(15, 15, 15)
                        .addComponent(jLabel_Preposto)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton_AtualizarCliente)
                            .addComponent(jTextField_Preposto, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Nome)
                    .addComponent(jTextField_Nome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_CPF)
                    .addComponent(jFormattedTextField_CPF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_RG)
                    .addComponent(jTextField_RG, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Endereco)
                    .addComponent(jTextField_Endereco, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_Bairro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_Bairro))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_Cidade, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_Cidade))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_CEP)
                    .addComponent(jLabel_UF)
                    .addComponent(jComboBox_UF, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextField_CEP, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_Visita)
                    .addComponent(jFormattedTextField_Visista, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel_Telefone)
                    .addComponent(jLabel_Celular)
                    .addComponent(jFormattedTextField_Telefone, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jFormattedTextField_Celular, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_Email, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_Email))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField_Preposto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel_Preposto))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton_AtualizarCliente)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButton_AtualizarClienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton_AtualizarClienteMouseClicked
        String nome = jTextField_Nome.getText();
        String cpf = jFormattedTextField_CPF.getText();
        String rg = jTextField_RG.getText();
        String endereco = jTextField_Endereco.getText();
        String bairro = jTextField_Bairro.getText();
        String cidade = jTextField_Cidade.getText();
        String cep = jFormattedTextField_CEP.getText();
        String uf = jComboBox_UF.getSelectedItem().toString();
        String telefone = jFormattedTextField_Telefone.getText();
        String celular = jFormattedTextField_Celular.getText();
        String email = jTextField_Email.getText();
        String visita = jFormattedTextField_Visista.getText();
        String preposto = jTextField_Preposto.getText();

        System.out.println("Verificando atributos.");
        if (cpf.trim().length() == 9) {
            cpf = "";
        }
        if (cep.trim().length() == 1) {
            cep = "";
        }
        if (telefone.trim().length() == 10) {
            telefone = "";
        }
        if (celular.trim().length() == 10) {
            celular = "";
        }
        if (visita.trim().length() == 4) {
            visita = "";
        }

        //Atualizando cliente
        cliente.setNome(nome);
        cliente.setBairro(bairro);
        cliente.setCelular(celular);
        cliente.setCep(cep);
        cliente.setCidade(cidade);
        cliente.setCpf(cpf);
        cliente.setEmail(email);
        cliente.setEndereco(endereco);
        cliente.setRg(rg);
        cliente.setTelefone(telefone);
        cliente.setUf(uf);
        cliente.setVisita(visita);
        cliente.setPreposto(preposto);

        try {
            advogadoFacade.atualizarCliente(cliente);
            JOptionPane.showMessageDialog(null, "Cliente " + nome + " atualizado com sucesso.");
        } catch (AtributoNaoEncontrado ex) {
            System.out.println("");
            System.out.println("Erro: Falta algum atributo.");
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
            if (ex.getMessage().contains("Nome")) {
                jTextField_Nome.setText("");
                jTextField_Nome.requestFocus();
            }
            if (ex.getMessage().contains("CPF")) {
                jFormattedTextField_CPF.setText("");
                jFormattedTextField_CPF.requestFocus();
            }
        } catch (CPFInvalidoException ex) {
            System.out.println("");
            System.out.println("Erro: CPF inválido.");
            JOptionPane.showMessageDialog(null, ex.getMessage(), "Erro", JOptionPane.ERROR_MESSAGE);
            jFormattedTextField_CPF.setText("");
            jFormattedTextField_CPF.requestFocus();
        }

    }//GEN-LAST:event_jButton_AtualizarClienteMouseClicked
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton_AtualizarCliente;
    private javax.swing.JComboBox jComboBox_UF;
    private javax.swing.JFormattedTextField jFormattedTextField_CEP;
    private javax.swing.JFormattedTextField jFormattedTextField_CPF;
    private javax.swing.JFormattedTextField jFormattedTextField_Celular;
    private javax.swing.JFormattedTextField jFormattedTextField_Telefone;
    private javax.swing.JFormattedTextField jFormattedTextField_Visista;
    private javax.swing.JLabel jLabel_Bairro;
    private javax.swing.JLabel jLabel_CEP;
    private javax.swing.JLabel jLabel_CPF;
    private javax.swing.JLabel jLabel_Celular;
    private javax.swing.JLabel jLabel_Cidade;
    private javax.swing.JLabel jLabel_Email;
    private javax.swing.JLabel jLabel_Endereco;
    private javax.swing.JLabel jLabel_Nome;
    private javax.swing.JLabel jLabel_Preposto;
    private javax.swing.JLabel jLabel_RG;
    private javax.swing.JLabel jLabel_Telefone;
    private javax.swing.JLabel jLabel_UF;
    private javax.swing.JLabel jLabel_Visita;
    private javax.swing.JTextField jTextField_Bairro;
    private javax.swing.JTextField jTextField_Cidade;
    private javax.swing.JTextField jTextField_Email;
    private javax.swing.JTextField jTextField_Endereco;
    private javax.swing.JTextField jTextField_Nome;
    private javax.swing.JTextField jTextField_Preposto;
    private javax.swing.JTextField jTextField_RG;
    // End of variables declaration//GEN-END:variables
}
